const API = require('../api/index')
const {
  typeJson
} = require('../../utils/typejson')
let prolist = wx.getStorageSync('prolist') == '' ? wx.getStorageSync('prolist') : JSON.parse(wx.getStorageSync('prolist'))
const {
  get,
  post
} = require('../../request/index')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    itemInfo: {},
    prolist: wx.getStorageSync('prolist') == '' ? wx.getStorageSync('prolist') : JSON.parse(wx.getStorageSync('prolist')),
    form: {
      checkPart: "",
      importTime: "",
      outTime: "",
      content: "",
      checkResult: "",
      exchangeSituation: "",
      remark: "",
      signPic: "",
    },
    parmes: {
      checkPart: "",
      importTime: "",
      outTime: "",
      content: "",
      checkResult: "",
      exchangeSituation: "",
      remark: "",
      signPic: "",
    },
    show: false,
    regionList: [{
      name: '检查站情况',
      ontype: 1
    },
    {
      name: '巡查点情况',
      ontype: 2
    },
    {
      name: '监控记录审查情况',
      ontype: 3
    },
    {
      name: '其他',
      ontype: 4
    }
    ],
  },
  // 时间 start 
  changeTime(e) {
    let value = 'parmes.importTime'; // 注意这里哈
    this.setData({
      [value]: e.detail
    })
  },
  changeTime1(e) {
    let value = 'parmes.outTime'; // 注意这里哈
    this.setData({
      [value]: e.detail
    })
  },
  // 时间 end
  // 签名
  canvasChang(e) {
    let value = 'parmes.signPic'; // 注意这里哈
    this.setData({
      [value]: e.detail
    })
  },
  // 签名
  changeUpint(e) {
    let value = 'parmes.checkPic'; // 注意这里哈
    this.setData({
      [value]: e.detail,
    });
  },
  onChange(event) {
    let value = "parmes.mainProcess"
    this.setData({
      [value]: event.detail,
    });
  },
  onChangeInput(e) {
    this.setData({
      [e.target.dataset.lable]: e.detail
    })
  },
  selent() {
    this.setData({
      show: true
    })
  },
  onClose() {
    this.setData({
      show: false,
    });
  },
  onSelect(event) {
    console.log(event.detail);
    let value = "form.checkPart"
    let label = "parmes.checkPart"
    this.setData({
      [value]: event.detail.name,
      [label]: event.detail.ontype,
      show: false
    })
  },
  submit() {
    post('/app/sitelog/getAlldata', {
      userId: this.data.prolist.userId,
      planId: this.data.itemInfo.id,
      siteType: 2
    }).then(res => {
      let flag = true
      let id = null
      let val = res.rows
      for (let index = 0; index < val.length; index++) {
        if (val[index].checkPart == this.data.parmes.checkPart) {
          flag = false;
          id = val[index].id;
        }
      }
      if (flag) { //新增
        let obj = Object.assign(this.data.parmes, {
          planId: this.data.itemInfo.id,
          userId: this.data.prolist.userId,
        })
        post('/app/sitelog/add', obj).then(res => {
          if (res.code == 200) {
            const pages = getCurrentPages()     //获取页面列表
            const perpage = pages[pages.length - 2]    //上一页   
            perpage.onLoad()
            wx.showToast({
              title: '提交成功',
              duration: 3000,
              icon: 'success',
              mask: true
            })
            wx.navigateBack({
              delta: 0,
            })
          }
        })

      } else {
        let obj = Object.assign(this.data.parmes, {
          planId: this.data.itemInfo.id,
          userId: this.data.prolist.userId,
          id: id
        })
        post('/app/sitelog/edit', obj).then(res => {
          if (res.code == 200) {
            const pages = getCurrentPages()     //获取页面列表
            const perpage = pages[pages.length - 2]    //上一页   
            perpage.onLoad()
            wx.showToast({
              title: '提交成功',
              duration: 3000,
              icon: 'success',
              mask: true
            })
            wx.navigateBack({
              delta: 0,
            })
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let info = JSON.parse(options.info)
    this.setData({
      itemInfo: info
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})